

//上传表单图片，详情请转至layui.upload
//https://fly.layui.com/jie/12987/


/*
上传参数设定
*/
var upurl = "/upload/upimg";//上传图片地址
var duotu = true;//是否为多图上传true false

/*
上传图片
*/
layui.use('upload', function() {
    upload = layui.upload;
    upload.render({
        elem: '#upload_img',
        url: upurl,
        multiple: duotu,
        before: function(obj) {
            layer.msg('图片上传中...', {
                icon: 16,
                shade: 0.01,
                time: 0
            })
        },
        done: function(res) {
            layer.close(layer.msg());//关闭上传提示窗口
            if (duotu == true) {//调用多图上传方法,其中res.imgid为后台返回的一个随机数字
                $('#upload_img_list').append('<dd class="item_img" id="' + res.imgid + '"><div class="operate"><i class="toleft layui-icon"></i><i class="toright layui-icon"></i><i onclick=UPLOAD_IMG_DEL("' + res.imgid + '") class="close layui-icon"></i></div><img src="' + res.tolink + '" class="img" ><input type="hidden" name="dzd_img[]" value="' + res.tolink + '" /></dd>');
            }else{//调用单图上传方法,其中res.imgid为后台返回的一个随机数字
                $('#upload_img_list').html('<dd class="item_img" id="' + res.imgid + '"><div class="operate"><i onclick=UPLOAD_IMG_DEL("' + res.imgid + '") class="close layui-icon"></i></div><img src="' + res.tolink + '" class="img" ><input type="hidden" name="dzd_img" value="' + res.tolink + '" /></dd>');
            }
        }
    })


    upload.render({
        elem: '#upload_img02',
        url: upurl,
        multiple: duotu,
        before: function(obj) {
            layer.msg('图片上传中...', {
                icon: 16,
                shade: 0.01,
                time: 0
            })
        },
        done: function(res) {
            layer.close(layer.msg());//关闭上传提示窗口
            if (duotu == true) {//调用多图上传方法,其中res.imgid为后台返回的一个随机数字
                $('#upload_img_list').append('<dd class="item_img" id="' + res.imgid + '"><div class="operate"><i class="toleft layui-icon"></i><i class="toright layui-icon"></i><i onclick=UPLOAD_IMG_DEL("' + res.imgid + '") class="close layui-icon"></i></div><img src="' + res.tolink + '" class="img" ><input type="hidden" name="dzd_img[]" value="' + res.tolink + '" /></dd>');
            }else{//调用单图上传方法,其中res.imgid为后台返回的一个随机数字
                $('#upload_img_list').html('<dd class="item_img" id="' + res.imgid + '"><div class="operate"><i onclick=UPLOAD_IMG_DEL("' + res.imgid + '") class="close layui-icon"></i></div><img src="' + res.tolink + '" class="img" ><input type="hidden" name="dzd_img" value="' + res.tolink + '" /></dd>');
            }
        }
    })
});


/*
删除上传图片
*/
function UPLOAD_IMG_DEL(divs) {
    $("#"+divs).remove();
}

/*
多图上传变换左右位置
*/
$(".toleft").live("click", function() {
    var item = $(this).parent().parent(".item_img");
    var item_left = item.prev(".item_img");
    if ($("#upload_img_list").children(".item_img").length >= 2) {
        if (item_left.length == 0) {
            item.insertAfter($("#upload_img_list").children(".item_img:last"))
        } else {
            item.insertBefore(item_left)
        }
    }
});
$(".toright").live("click", function() {
    var item = $(this).parent().parent(".item_img");
    var item_right = item.next(".item_img");
    if ($("#upload_img_list").children(".item_img").length >= 2) {
        if (item_right.length == 0) {
            item.insertBefore($("#upload_img_list").children(".item_img:first"))
        } else {
            item.insertAfter(item_right)
        }
    }
});